<?php

require './site/dkp/dkpfunctions.php';
require './site/dkp/dkpChartData.php';
if (isset($_GET['refresh']))
	refreshDKPLists();
		
global $pun_user;

showPageHeader();
showIntroText();
/*if (isset($_GET['m']) && ($pun_user['g_id'] == 1 || $pun_user['g_id'] == 2))
{
	showChartAttendance();
} else {

}*/
showDetail();
showPageFoot();

// -------------------------------------------------------------
//  ----  Functions  -----------------------------------------
// -------------------------------------------------------------
function showRefreshLink() {
   	echo '<br /><img src="/images/common/icons/arrow_refresh.png" style="vertical-align:middle;"/>&nbsp;<a href="index.php?page=dkptables&refresh" title="Refresh DKP lists" style="vertical-align:middle;">Refresh</a>';
}
// -------------------------------------------------------------
function refreshDKPLists() {
   	global $db;
	$db->query('
		TRUNCATE TABLE dmzdkp_memberStandingStatic;
	') or error('Unable to truncate table', __FILE__, __LINE__, $db->error());
	$db->query('
		INSERT INTO dmzdkp_memberStandingStatic (MemberID, DKP, Bonus, Standing)
		SELECT * FROM dmzdkp_memberStanding;
	') or error('Unable to refresh DKP', __FILE__, __LINE__, $db->error());
}
// -------------------------------------------------------------
function showChartAttendance()
{
	/*
getChartDataAttendance
	*/


	$file = './site/dkp/flatFiles/chartAttendance_'.$_GET[m].'.txt';
	$fh = fopen($file, "w");
	$fileContent = '';
	$dataArray = getChartDataAttendance($_GET[m]);
	$fileContent = $dataArray;
	fwrite($fh, $fileContent);
	fclose($fh);

?>
<script language="JavaScript" type="text/javascript">
<!--
if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
	alert("This page requires AC_RunActiveContent.js.");
} else {
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if(hasRightVersion) {
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0',
			'width', '535',
			'height', '250',
			'scale', 'noscale',
			'salign', 'TL',
			'bgcolor', '#777788',
			'wmode', 'opaque',
			'movie', 'charts',
			'src', 'charts',
			'FlashVars', 'library_path=site/dkp/charts_library&xml_source=<?php echo $file ?>',
			'id', 'my_chart',
			'name', 'my_chart',
			'menu', 'true',
			'allowFullScreen', 'true',
			'allowScriptAccess','sameDomain',
			'quality', 'high',
			'align', 'middle',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'play', 'true',
			'devicefont', 'false'
			);
	} else {
		var alternateContent = 'This content requires the Adobe Flash Player. '
		+ '<u><a href=http://www.macromedia.com/go/getflash/>Get Flash</a></u>.';
		document.write(alternateContent);
	}
}
// -->
</script>
<noscript>
	<P>This content requires JavaScript.</P>
</noscript>
<?php
}
// -------------------------------------------------------------
function showChartDKP()
{
	$file = './site/dkp/flatFiles/chartDKP_'.$_GET[m].'.txt';
	$fh = fopen($file, "w");
	$fileContent = '';
	$dataArray = getChartDataDKP($_GET[m]);
	$fileContent = $dataArray;
	fwrite($fh, $fileContent);
	fclose($fh);

?>
<script language="JavaScript" type="text/javascript">
<!--
if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
	alert("This page requires AC_RunActiveContent.js.");
} else {
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if(hasRightVersion) {
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0',
			'width', '535',
			'height', '250',
			'scale', 'noscale',
			'salign', 'TL',
			'bgcolor', '#777788',
			'wmode', 'opaque',
			'movie', 'charts',
			'src', 'charts',
			'FlashVars', 'library_path=site/dkp/charts_library&xml_source=<?php echo $file ?>',
			'id', 'my_chart',
			'name', 'my_chart',
			'menu', 'true',
			'allowFullScreen', 'true',
			'allowScriptAccess','sameDomain',
			'quality', 'high',
			'align', 'middle',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'play', 'true',
			'devicefont', 'false'
			);
	} else {
		var alternateContent = 'This content requires the Adobe Flash Player. '
		+ '<u><a href=http://www.macromedia.com/go/getflash/>Get Flash</a></u>.';
		document.write(alternateContent);
	}
}
// -->
</script>
<noscript>
	<P>This content requires JavaScript.</P>
</noscript>
<?php
}
// -------------------------------------------------------------
function showPageHeader()
/*
Opens the pages middle section content box and adds the title
*/
{
?>
    	<div id="content">
			<?php showDKPTitlebar('DKP Tables - Beta'); ?>
            <div class="box">
                <div class="contents">
<?php
}
// -------------------------------------------------------------
function showPageFoot()
/*
Displays final links and closes the pages middle section content box
*/
{
?>
                </div>
            </div>
    	</div>
<?php
}
// -------------------------------------------------------------
function showIntroText() {
/*
Displays the text at the top of the page
*/
global $pun_user;
	?>
	<div style="background-image:url(./site/dkp/images/wh-power.png); background-repeat:no-repeat; background-position:right; padding: 1px 1px 8px 1px;margin: 2px 0px 2px 2px; text-align: left;">
		<p style="padding: 5px 10px 10px 4px;">
			<?php
			if (isset($_GET['m'])) {
				$namesArray = getRaiderNamesArray();
				//print_r($namesArray);
				echo 'Below is the raid history for '.$namesArray[$_GET['m']].'.';
			} elseif (isset($_GET['b'])) {
				echo 'Below is the DMZ DKP breakdowns.<br />
						<br />
						<img src="/images/common/icons/page_white_go.png"  style="vertical-align:middle;"/>&nbsp;<a href="index.php?page=dkptables" style="vertical-align:middle;">View DKP standings list</a>';
			} else {
				echo 'Below is the DMZ DKP standings list.<br />
						<br />
						<img src="/images/common/icons/page_white_go.png"  style="vertical-align:middle;"/>&nbsp;<a href="index.php?page=dkptables&b=1" style="vertical-align:middle;">View DKP breakdowns</a>';
			}
			if (isEditable())
				showRefreshLink();
			?>
		</p>
	</div>
	<?php
}
// -------------------------------------------------------------
function showDetail()
{
	if (isset($_GET['m'])) {
		showRaiderDetails($_GET['m']);
	} elseif (isset($_GET['b'])) {
		echo '<p style="font-weight: bold; font-size: 14px;">Breakdown by tier token:</p>';
		showDKPByTier();
		echo '<p style="font-weight: bold; font-size: 14px;">Breakdown by role:</p>';
		showDKPByRole();
		echo '<p style="font-weight: bold; font-size: 14px;">Breakdown by class:</p>';
		showDKPByClass();
	} else {
		showStandingTable();
		global $pun_user;
		if ($pun_user['id'] == 2) {
			//$tierDrops = getTierDrops(75);
			//print_r($tierDrops);
		}
	}
}
// -------------------------------------------------------------
function showDKPByRole()
{
	$order = 'RoleID, Standing DESC';
	$standings = getStandingsArray($order);
	$roleTable = 'none';
	echo '<table cellspacing="0"><tr>';
	foreach ($standings as $member){
		if ($member[Enabled] == 1) {
			if ($member[RoleName] != $roleTable) {
				if ($roleTable != 'none')
					echo '</table></td>';
				$roleTable = $member[RoleName];
				echo '
				<td width="130px" style="vertical-align: top; border: 1px solid black">
				<table cellspacing="0">
					<tr class="tablehead">
						<th style="color: white; text-align: center; padding: 0px; margin: 0px; border-bottom: 1px solid black; background-color: #666;" colspan="2">
							&nbsp;'.$roleTable.'</th>
					</tr>
				'; //<img src="./img/clandmz/'.$member[ClassImage].'" height="15px" width="15px" title="'.$member[ClassName].'" style="border: 0px; padding: 0px" />&nbsp;<span style="color: #'.$member[ClassColour].'; font-weight: bold;">'.$member[ClassName].'</span>
			}
			?>	<tr bgcolor="#f1f1f1" onMouseOver="this.bgColor='#ccb';" onMouseOut="this.bgColor='#f1f1f1';"> <?php
			echo '
				<td style="text-align: left; padding: 2px">
					<a href="index.php?page=dkptables&m='.$member[MemberID].'" style="color: #'.$member[ClassColour].'; font-weight: bold; text-decoration: none">
						'.$member[MemberName].'
					</a>
				</td>
				<td width="35px" style="text-align: right; padding: 2px">
					'.round($member[Standing]).'
				</td>
			</tr>
			'; //<img src="'.$member[RoleImage].'" height="15px" width="15px" title="'.$member[RoleName].'" style="border: 0px; padding: 0px" />
		}
	}
	echo '</table></td></tr></table><br />';
}
// -------------------------------------------------------------
function showDKPByTier()
{
	$tierVanquisher = array('Death Knight', 'Druid', 'Mage', 'Rogue');
	$tierProtector = array('Hunter', 'Shaman', 'Warrior');
	$tierConqueror = array('Paladin', 'Priest', 'Warlock');
	$standingVanquisher = array();
	$standingProtector = array();
	$standingConqueror = array();
	
	global $pun_user;
	if ($pun_user['id'] == 2) {
		$tierDrops = getTierDrops(75);
	}

	$order = 'Standing DESC, DKP DESC';
	$standings = getStandingsArray($order);
//	print_r($standings);
	foreach ($standings as $member){
		if ($member[Enabled] == 1) {
			if (in_array($member[ClassName], $tierVanquisher)) {
				$standingVanquisher[] = array(
											'tierName' => 'Vanquisher',
											'MemberName' => $member[MemberName],
											'MemberID' => $member[MemberID],
											'ClassColour' => $member[ClassColour],
											'Standing' => $member[Standing],
										);
			} elseif (in_array($member[ClassName], $tierProtector)) {
				$standingProtector[] = array(
											'tierName' => 'Protector',
											'MemberName' => $member[MemberName],
											'MemberID' => $member[MemberID],
											'ClassColour' => $member[ClassColour],
											'Standing' => $member[Standing],
										);
			} elseif (in_array($member[ClassName], $tierConqueror)) {
				$standingConqueror[] = array(
											'tierName' => 'Conqueror',
											'MemberName' => $member[MemberName],
											'MemberID' => $member[MemberID],
											'ClassColour' => $member[ClassColour],
											'Standing' => $member[Standing],
										);
			}
		}
	}	
	$tierStanding = array();
	foreach ($standingVanquisher as $member){
		$tierStanding[] = array(
							'tierName' => 'Vanquisher',
							'MemberName' => $member[MemberName],
							'MemberID' => $member[MemberID],
							'ClassColour' => $member[ClassColour],
							'Standing' => $member[Standing],
						);
	}
	foreach ($standingProtector as $member){
		$tierStanding[] = array(
							'tierName' => 'Protector',
							'MemberName' => $member[MemberName],
							'MemberID' => $member[MemberID],
							'ClassColour' => $member[ClassColour],
							'Standing' => $member[Standing],
						);
	}
	foreach ($standingConqueror as $member){
		$tierStanding[] = array(
							'tierName' => 'Conqueror',
							'MemberName' => $member[MemberName],
							'MemberID' => $member[MemberID],
							'ClassColour' => $member[ClassColour],
							'Standing' => $member[Standing],
						);
	}

	$tierTable = 'none';
	echo '<table cellspacing="0"><tr>';
	foreach ($tierStanding as $member){
		if ($member[tierName] != $tierTable) {
			if ($tierTable != 'none')
				echo '</table></td>';
			$tierTable = $member[tierName];
			echo '
			<td width="130px" style="vertical-align: top; border: 1px solid black">
			<table cellspacing="0">
				<tr class="tablehead">
					<th style="color: white; text-align: center; padding: 0px; margin: 0px; border-bottom: 1px solid black; background-color: #666;" colspan="2">
						&nbsp;'.$tierTable.'</th>
				</tr>
			'; //<img src="./img/clandmz/'.$member[ClassImage].'" height="15px" width="15px" title="'.$member[ClassName].'" style="border: 0px; padding: 0px" />&nbsp;<span style="color: #'.$member[ClassColour].'; font-weight: bold;">'.$member[ClassName].'</span>
		}
		?>	<tr bgcolor="#f1f1f1" onMouseOver="this.bgColor='#ccb';" onMouseOut="this.bgColor='#f1f1f1';"> <?php
		echo '
			<td style="text-align: left; padding: 2px">
				<a href="index.php?page=dkptables&m='.$member[MemberID].'" style="color: #'.$member[ClassColour].'; font-weight: bold; text-decoration: none">
					'.$member[MemberName].'
				</a>
			</td>
			<td width="35px" style="text-align: right; padding: 2px">
				';
/*		foreach ($tierDrops as $tierDrop) {
			if ($tierDrop[MemberID] == $member[MemberID])
				echo '|';
		}
*/		echo	round($member[Standing]).'
			</td>
		</tr>
		'; //<img src="'.$member[RoleImage].'" height="15px" width="15px" title="'.$member[RoleName].'" style="border: 0px; padding: 0px" />
	}
	echo '</table></td></tr></table><br />';
}
// -------------------------------------------------------------
function showDKPByClass()
{
	$order = 'ClassName, Standing DESC';
	$standings = getStandingsArray($order);
	$classTable = 'none';
	echo '<table cellspacing="0"><tr>';
	foreach ($standings as $member){
		if ($member[Enabled] == 1) {
			if ($member[ClassName] != $classTable) {
				if ($classTable != 'none')
					echo '</table></td>';
				$classTable = $member[ClassName];
				if ($classTable == 'Paladin' || $classTable == 'Warlock')
					echo '</tr><tr>';
				if ($classTable == 'Warlock')
					echo '<td>&nbsp;</td>';
				echo '
				<td width="130px" style="vertical-align: top; border: 1px solid black">
				<table cellspacing="0">
					<tr class="tablehead">
						<th style="color: white; text-align: center; padding: 0px; margin: 0px; border-bottom: 1px solid black; background-color: #666;" colspan="2">
							&nbsp;'.$classTable.'</th>
					</tr>
				'; //<img src="./img/clandmz/'.$member[ClassImage].'" height="15px" width="15px" title="'.$member[ClassName].'" style="border: 0px; padding: 0px" />&nbsp;<span style="color: #'.$member[ClassColour].'; font-weight: bold;">'.$member[ClassName].'</span>
			}
			?>	<tr bgcolor="#f1f1f1" onMouseOver="this.bgColor='#ccb';" onMouseOut="this.bgColor='#f1f1f1';"> <?php
			echo '
				<td style="text-align: left; padding: 2px">
					<a href="index.php?page=dkptables&m='.$member[MemberID].'" style="color: #'.$member[ClassColour].'; font-weight: bold; text-decoration: none">
						'.$member[MemberName].'
					</a>
				</td>
				<td width="35px" style="text-align: right; padding: 2px">
					'.round($member[Standing]).'
				</td>
			</tr>
			'; //<img src="'.$member[RoleImage].'" height="15px" width="15px" title="'.$member[RoleName].'" style="border: 0px; padding: 0px" />
		}
	}
	echo '</table></td></tr></table><br />';
}
// -------------------------------------------------------------
/* -------------------------------------------------------------
		$standingsArray[] = array(
			'MemberID' => $cur_row['MemberID'], 
			'DKP' => $cur_row['DKP'], 
			'Bonus' => $cur_row['Bonus'], 
			'Standing' => $cur_row['Standing'], 
			'MemberName' => $cur_row['MemberName'], 
			'ClassID' => $cur_row['ClassID'], 
			'ClassName' => $cur_row['ClassName'], 
			'ClassColour' => $cur_row['ClassColour'], 
			'ClassImage' => $cur_row['ClassImage'], 
			'RoleID' => $cur_row['RoleID'], 
			'RoleName' => $cur_row['RoleName'], 
			'RoleImage' => $cur_row['RoleImage'], 
			'Enabled' => $cur_row['Enabled'], 
			);
 -------------------------------------------------------------*/
?>
